//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


namespace Shp.Runtime.OfflineSitka
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Shp.Runtime.Sitka.ServiceGateway.SitkaClient;
    
    internal class Validations
    {
        /// <summary>
        /// Simple, incomplete syntax validation
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        internal static void IsValidateQueryStatement(string query)
        {
            string q = query.Replace("from e in entities", "");
            q = q.Replace("select e", "").Trim();

            if (q.StartsWith("where", StringComparison.Ordinal) || string.IsNullOrEmpty(q))
            {
                return;
            }

            throw new ArgumentException("Query statement: (" + query + ") is invalid");
        }

        /// <summary>
        /// Checks that a container exists
        /// </summary>
        /// <param name="conatinerId"></param>
        internal static void IsValidContainer(string conatinerId)
        {
            int qty = OfflineSitkaSql.GetContainer(conatinerId);

            if (qty == 0)
            {
                throw new ArgumentException("Invalid Container");
            }
        }

        /// <summary>
        /// Check the entityid passed is valid (not null or emtpty)
        /// </summary>
        /// <param name="EntityId"></param>
        internal static void IsValidEntity(string entityId)
        {
            if (string.IsNullOrEmpty(entityId))
            {
                throw new ArgumentException("An entity Id must be provided");
            }
        }

        /// <summary>
        /// Checks the FlexibleEntity is actually and "Entity"
        /// </summary>
        /// <param name="entity"></param>
        internal static void IsAnEntity(Entity entity)
        {
            if (entity is Entity)
            {
                return;
            }

            throw new ArgumentException(entity.GetType().ToString() + " is unsupported. Should be am Entity.");
        }
    }
}
